#!/usr/bin/env python
# -*- coding: utf8 -*-

import sys,optparse,commands


def options():
    usage = '''Use like: 
%s -w 20%%
%s -w 400
''' % (__file__,__file__)
   
    parser = optparse.OptionParser(usage)
    parser.add_option('-w','--warnning',dest='war',help='Use%')
    return parser

def convert(size,unit=''):
    size = float(size)
    if unit:
        return (size/1024)
    if size > 1024:
        size = size/1024
        if size > 1024:
            size = '%.2fGB' % (size/1024)
        else:
            size = '%.0fMB' % size
    else:
        size = '%sKB' % size
    return size

def main():
    parser = options()
    opt,args = parser.parse_args()
    if opt.war: 
        if opt.war.find('%') != -1:
            useqwar = int(opt.war.replace('%',''))
            usewar = ''
        else:
            useqwar = ''
            usewar = int(opt.war)
        stat = 0
        msg = ''
        cmdoutput = '|'
        status,output = commands.getstatusoutput("df -Phlk -x tmpfs -x iso9660 |awk '{print $NF,$(NF-4),$(NF-3)}'|grep '^/'")
        if status != 0:
            print 'Failed: ',output
            sys.exit(2)
        for temp in output.strip().split('\n'):
            mounted,sizetotal,sizeuse = temp.split()
            useq = int(sizeuse)*100/int(sizetotal)
            msg += '%s total:%s use:%s;' % (mounted,convert(sizetotal),convert(sizeuse))
            cmdoutput += '%s:%s:%s;' % (mounted,useq,opt.war)
            if usewar:
                if convert(sizeuse,unit='MB') > usewar:stat += 1
            else:
                if useq > useqwar:stat += 1
        print msg.rstrip(';')+cmdoutput.rstrip(';').replace('/','X')
        if stat > 0:
            sys.exit(1)
        else:
            sys.exit(0)
    else:
        parser.print_help()

if __name__=="__main__":
    main()
